package com.jymj.service.impl;

import com.jymj.entity.KeyValueEntity;
import com.jymj.entity.TdlyEntity;
import com.jymj.entity.YlEntity;
import com.jymj.entity.huji.FeiZhaiEntity;
import com.jymj.entity.huji.ZhaiEntity;
import com.jymj.entity.zjd.ZjdCountEntity;
import com.jymj.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * Created by ljj on 2017/12/18.
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class PointServiceImpl implements PointService {

    @Resource
    private ZhaiService zhaiService;
    @Resource
    private FeiZhaiService feiZhaiService;
    @Resource
    private YlService ylService;
    @Resource
    private SysXzqService xzqService;
    @Resource
    private LandService landService;
    @Autowired
    private ZjdSqService zjdSqService;
    @Autowired
    private ZjdLzService zjdLzService;
    @Autowired
    private ApplyService applyService;
    @Autowired
    private ExitService exitService;
    @Autowired
    private IllegalService illegalService;

    @Override
    public Map getQueryPoint(String point, String code) {
        Map map = new HashMap();
        YlEntity ylEntity = ylService.getYLbyPoint(point, code);
        if (ylEntity != null && ylEntity.getObjectid() != null) {
            if (ylEntity.getYlType() != null && (ylEntity.getYlType() == 1 || ylEntity.getYlType() == 4)) {
                List<ZhaiEntity> zhaiList = zhaiService.getZhaiEntity(ylEntity.getObjectid(),null);
                if (zhaiList.size() > 0) {
                    map.put("zhaiList", zhaiList);
                }
            } else {
                List<FeiZhaiEntity> feiList = feiZhaiService.getFeiZhaiEn(ylEntity.getObjectid());
                List<TdlyEntity> tdlyList = landService.getTdlyList(ylEntity.getObjectid());
                map.put("tdlyList", tdlyList);
                map.put("feiList", feiList);

            }
            /*by dl 2018年3月29日 不需要房屋信息
            List<FWEntity> fwList = fwService.getfFwList(ylEntity.getObjectid());
            if(fwList.size()>0){
                map.put("fwList",fwList);
            }
             */
            map.put("ylEntity", ylEntity);
        }
        return map;
    }

    @Override
    public Map getAppHuQuery(Integer[] sexList1, Integer[] empList1, Integer[] huTList1, Integer startAge, Integer endAge, String housecount, String idCard, List code) {
        Map map = new HashMap();
        ZhaiEntity zhai = new ZhaiEntity();
        if (sexList1 != null && sexList1.length > 0) {
            List list = Arrays.asList(sexList1);
            zhai.setSexList(list);
        }
        if (empList1 != null && empList1.length > 0) {
            List list = Arrays.asList(empList1);
            zhai.setEmpList(list);
        }
        if (huTList1 != null && huTList1.length > 0) {
            List list = Arrays.asList(huTList1);
            zhai.setHuTList(list);
        }
        if (startAge != null) {
            zhai.setStartAge(startAge);
        }
        if (endAge != null) {
            zhai.setEndAge(endAge);
        }
        if (housecount != null && !housecount.equals("")) {
            zhai.setSocialrelat(1);
            zhai.setHousecount(housecount);
        }
        if (idCard != null && !idCard.equals("")) {
            zhai.setIdCard(idCard);
        }
        List<KeyValueEntity> ageList = zhaiService.queryByAge(zhai, code);
        List<KeyValueEntity> sexList = zhaiService.queryBysex(zhai, code);
        List<KeyValueEntity> empList = zhaiService.queryByEmp(zhai, code);
        List<KeyValueEntity> huTList = zhaiService.queryByHuType(zhai, code);
        ageList.removeAll(Collections.singleton(null));
        sexList.removeAll(Collections.singleton(null));
        empList.removeAll(Collections.singleton(null));
        huTList.removeAll(Collections.singleton(null));
        map.put("ageList", ageList);
        map.put("sexList", sexList);
        map.put("empList", empList);
        map.put("huTList", huTList);
        return map;
    }

    @Override
    public Map getAppDiQuery(String mph, Integer[] qsxzList1, Integer[] syqlxList1, Integer[] cqlyList1, String code) {
        Map map = new HashMap();
        YlEntity ylEntity = new YlEntity();
        if (mph != null && !mph.equals("")) {
            ylEntity.setMph(mph);
        }
        if (qsxzList1 != null && qsxzList1.length > 0) {
            List list = Arrays.asList(qsxzList1);
            ylEntity.setQsxzList(list);
        }
        if (syqlxList1 != null && syqlxList1.length > 0) {
            List list = Arrays.asList(syqlxList1);
            ylEntity.setSyqlxList(list);
        }
        if (cqlyList1 != null && cqlyList1.length > 0) {
            List list = Arrays.asList(cqlyList1);
            ylEntity.setCqlyList(list);
        }
        Integer allCount = ylService.getAppDiCounnt(ylEntity, code);
        List<KeyValueEntity> qsxzList = ylService.getAppDiQsxz(ylEntity, code);
        List<KeyValueEntity> syqlxList = ylService.getAppDiSyqlx(ylEntity, code);
        List<KeyValueEntity> cqlyList = ylService.getAppDiCqly(ylEntity, code);
        qsxzList.removeAll(Collections.singleton(null));
        syqlxList.removeAll(Collections.singleton(null));
        cqlyList.removeAll(Collections.singleton(null));
        map.put("allCount", allCount);
        map.put("qsxzList", qsxzList);
        map.put("syqlxList", syqlxList);
        map.put("cqlyList", cqlyList);
        return map;
    }

    @Override
    public Map getZhaiQueryFrame(Integer pageSize, Integer currPage, String points, String code) {
        Map map = new HashMap();
        List<ZhaiEntity> zhaiList;
        Map map1 = new HashMap();
        if (pageSize != null && currPage != null) {
            map1.put("pageSize", pageSize);
            map1.put("currPage", currPage);
            map1.put("points", points);
            map1.put("code", code);
            zhaiList = zhaiService.getZhaiList(map1);//明细
            List<KeyValueEntity> se1List = zhaiService.getse1ByFrame(points, code);
            List<KeyValueEntity> se2List = zhaiService.getse2ByFrame(points, code);
            List<KeyValueEntity> se0List = zhaiService.getse0ByFrame(points, code);
            List<KeyValueEntity> empList = zhaiService.queryByEmp(points, code);
            List<KeyValueEntity> huTypeList = zhaiService.queryByHuType(points, code);
            se1List.removeAll(Collections.singleton(null));
            se2List.removeAll(Collections.singleton(null));
            se0List.removeAll(Collections.singleton(null));
            empList.removeAll(Collections.singleton(null));
            huTypeList.removeAll(Collections.singleton(null));
            if (se1List.size() > 0) {
                map.put("se1List", se1List);
            }
            if (se2List.size() > 0) {
                map.put("se2List", se2List);
            }
            if (se0List.size() > 0) {
                map.put("se0List", se0List);
            }
            if (empList.size() > 0) {
                map.put("empList", empList);
            }
            if (huTypeList.size() > 0) {
                map.put("huTypeList", huTypeList);
            }
        } else {
            map1.put("points", points);
            map1.put("code", code);
            zhaiList = zhaiService.getZhaiListExcel(map1);//明细
        }
        Integer zhaiCount = zhaiService.getZhaiListCount(points, code);
        zhaiList.removeAll(Collections.singleton(null));
        if (zhaiList.size() > 0) {
            map.put("zhaiList", zhaiList);
        }
        map.put("zhaiCount", zhaiCount);
        return map;
    }

    @Override
    public Map getAppQueryFrame(String points, String code) {
        Map map = new HashMap();
        List<KeyValueEntity> ylList = ylService.getZhaiAreaByFrame(points, code);
        List<KeyValueEntity> se1List = zhaiService.getse1ByFrame(points, code);
        List<KeyValueEntity> se2List = zhaiService.getse2ByFrame(points, code);
        List<KeyValueEntity> se0List = zhaiService.getse0ByFrame(points, code);
        List<KeyValueEntity> empList = zhaiService.queryByEmp(points, code);
        List<KeyValueEntity> huTypeList = zhaiService.queryByHuType(points, code);
        ylList.removeAll(Collections.singleton(null));
        se1List.removeAll(Collections.singleton(null));
        se2List.removeAll(Collections.singleton(null));
        se0List.removeAll(Collections.singleton(null));
        empList.removeAll(Collections.singleton(null));
        huTypeList.removeAll(Collections.singleton(null));
        if (ylList.size() > 0) {
            map.put("ylList", ylList);
        }
        if (se1List.size() > 0) {
            map.put("se1List", se1List);
        }
        if (se2List.size() > 0) {
            map.put("se2List", se2List);
        }
        if (se0List.size() > 0) {
            map.put("se0List", se0List);
        }
        if (empList.size() > 0) {
            map.put("empList", empList);
        }
        if (huTypeList.size() > 0) {
            map.put("huTypeList", huTypeList);
        }
        return map;
    }

    @Override
    public Map getYlQueryFrame(Integer pageSize, Integer currPage, String points, String code) {
        Map map = new HashMap();
        List<YlEntity> ylEntityList;
        //宅基地
        List ylTypeList = new ArrayList();
        ylTypeList.add(1);
        ylTypeList.add(4);
        Map map1 = new HashMap();
        if (pageSize != null && currPage != null) {
            map1.put("pageSize", pageSize);
            map1.put("currPage", currPage);
            map1.put("points", points);
            map1.put("code", code);
            map1.put("ylTypeList", ylTypeList);
            ylEntityList = ylService.getYlList(map1);//明细
        } else {
            map1.put("points", points);
            map1.put("code", code);
            map1.put("ylTypeList", ylTypeList);
            ylEntityList = ylService.getYlListExcel(map1);//明细
        }
        ylEntityList.removeAll(Collections.singleton(null));
        Integer ylCount = ylService.getYlListCount(points, code, ylTypeList);
        if (ylEntityList.size() > 0) {
            map.put("ylEntityList", ylEntityList);
        }
        map.put("ylCount", ylCount);
        return map;
    }

    @Override
    public Map queryZjdCount(String point,String code) {
        Map map = new HashMap();
        ZjdCountEntity sqzjd = zjdSqService.queryAllCount(point,code);
        ZjdCountEntity lzzjd = zjdLzService.queryAllCount(point,code);
        ZjdCountEntity fjzjd = applyService.queryAllCount(point,code);
        ZjdCountEntity tczjd = exitService.queryAllCount(point,code);
        ZjdCountEntity zjdillegal = illegalService.queryIllCount(point,1,code);
/*        ZjdCountEntity tdillegal = illegalService.queryIllCount(point,2,code);
        ZjdCountEntity envior = enviorSupvsService.queryAllCount(point);*/
        map.put("sqzjd",sqzjd);
        map.put("lzzjd",lzzjd);
        map.put("fjzjd",fjzjd);
        map.put("tczjd",tczjd);
        map.put("zjdillegal",zjdillegal);
/*        map.put("tdillegal",tdillegal);
        map.put("envior",envior);*/
        return map;
    }
}
